package com.ltsh.fim.client.handler;

import com.ltsh.fim.common.impl.MyDecoderImpl;
import com.ltsh.fim.common.inf.MyDecoder;
import com.ltsh.fim.common.model.FimFuture;
import com.ltsh.fim.common.model.FimRequest;
import com.ltsh.fim.common.model.FimRequestHolder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;

/**
 * Created by root on 1/11/18.
 */
public class FimClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    private MyDecoder myDecoder = new MyDecoderImpl();
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
        FimRequest request = myDecoder.decoding(packet.content());

        FimFuture rpcFuture = FimRequestHolder.get(String.valueOf(request.getId()));
        rpcFuture.done(request);
        System.out.println("client>>>" + request.getData());
//        ctx.close();
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        ctx.close();
        cause.printStackTrace();
    }
}